﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.IO;

using InfFront.UICore;

namespace InfFront.InfoSources.Base
{
    /// <summary>
    /// Image stream presenter
    /// </summary>
    public interface IImageSource
    {
        /// <summary>
        /// Image stream retrieval method
        /// </summary>
        /// <param name="width">desired image width</param>
        /// <param name="height">desired image height</param>
        /// <param name="style">desired image style</param>
        /// <returns>Image stream seeked to the start of image data</returns>
        Stream GetStretched(int width, int height, IconStyle style);
    }

    internal class ImageRecord : IImage
    {
        public string Uid { get; set; }
        public Stream Data { get; set; }

        #region IImage Members

        string IImage.GetUid()
        {
            return Uid;
        }

        Stream IImage.GetData()
        {
            return Data;
        }

        #endregion
    }

    [System.ComponentModel.ListBindable(false)]
    public class IconStack : List<IconStackEntry>, IImageSource
    {
        public IconStack()
        {
        }

        /// <summary>
        /// Image stream retrieval method
        /// </summary>
        /// <param name="width">desired image width</param>
        /// <param name="height">desired image height</param>
        /// <param name="style">desired image style</param>
        /// <returns>Image PNG-encoded stream seeked to the image data start</returns>
        public Stream GetStretched(int width, int height, IconStyle style)
        {
            IconStackEntry result = null;
            foreach (var item in this)
            {
                if (item.Image == null)
                    continue;

                if (item.Image.Width == width && item.Image.Height == height)
                {
                    result = item;
                }
            }

            if (result == null && Count > 0)
            {
                //throw new NotSupportedException("Image stretching is not supported yet");
                result = this[0];
            }

            if (result != null)
            {
                var stream = new System.IO.MemoryStream();
                result.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                stream.Seek(0, SeekOrigin.Begin);
                return stream;
            }

            return null;
        }
    }

    public class IconStackEntry
    {
        public IconStackEntry()
        {
            Style = IconStyle.Normal;
        }

        public Image Image { get; set; }

        [DefaultValue(IconStyle.Normal)]
        public IconStyle Style { get; set; }

        public override bool Equals(object obj)
        {
            var other = obj as IconStackEntry;
            if (other == null)
                return false;
            
            if (other.Style != Style || (other.Image == null) != (Image == null))
                return false;
            
            if (Image == null)
                return true;

            return Image.Equals(other.Image);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
